package de.mahosoftware.gwt.games.durak.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.mahosoftware.gwt.games.durak.client.module.Card;
import de.mahosoftware.gwt.games.durak.client.module.Card.Color;
import de.mahosoftware.gwt.games.durak.client.module.Player;
import de.mahosoftware.gwt.games.durak.util.CardDealer;

public class Game extends DockLayoutPanel {

	CardDealer cd = new CardDealer();
	List<Player> players = new ArrayList<Player>();
	Player testPlayer;

	public Game(Unit unit) {
		super(unit);
		testData();
		init();
	}

	private void testData() {
		testPlayer = new Player("Marcel");
		players.add(testPlayer);
		cd.giveCards(6, players);
	}

	private void init() {
		createNorth(20, testPlayer);
		createSouth(30, testPlayer);
		createWest(20, testPlayer);
		createEast(20, testPlayer);
		createCenter();
	}

	private void createNorth(double size, Player player) {
		FlowPanel f = new FlowPanel();
		f.add(new Label("CenterNorth"));
		addNorth(f, size);
	}

	private void createSouth(double size, Player player) {
		DockLayoutPanel dlp = new DockLayoutPanel(Unit.PCT);
		{// TODO: Buttons
			VerticalPanel vp = new VerticalPanel();
			vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			Button btn;
			
			for (int i = 0; i < 5; i++) {
				btn = new Button("Button " + i);
				btn.setWidth("120px");
				vp.add(btn);
			}
			dlp.addEast(vp, 20);
			dlp.addWest(new VerticalPanel(), 20);
		}
		FlowPanel f = new FlowPanel();
		player.sortCards();
		for (Card c : player.getHand()) {
			f.add(new Image(c.getRessource()));
		}

		f.add(new Label("CenterSouth"));
		dlp.add(f);
		addSouth(dlp, size);
	}

	private void createWest(double size, Player player) {
		FlowPanel f = new FlowPanel();
		f.add(new Label("CenterWest"));
		addWest(f, size);
	}

	private void createEast(double size, Player player) {
		FlowPanel f = new FlowPanel();
		f.add(new Label("CenterEast"));
		addEast(f, size);
	}

	private void createCenter() {
		FlowPanel f = new FlowPanel();
		f.add(new Label("Pot"));

		Image img;
		Card c;
		int counter = 0;
		for (int i = 2; i <= 14; i++) {
			if (counter < 3)
				counter++;
			else
				counter = 0;
			Color color = Color.values()[counter];
			c = new Card(i, color);
			img = new Image(c.getRessource());
			img.setSize("10%", "30%");
			f.add(img);
		}

		add(f);
	}

}
